package com.yang.service.wximpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketFootPrint;
import com.yang.bean.MarketFootPrintExample;
import com.yang.bean.MarketGoods;
import com.yang.bean.MarketGoodsExample;
import com.yang.bean.common.BaseParam;
import com.yang.bean.common.CommonData;
import com.yang.mapper.MarketFootPrintMapper;
import com.yang.mapper.MarketGoodsMapper;
import com.yang.service.wx.WXFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName WXFootprintServiceImpl
 * @Description TODO
 * @Author zhangdongdong
 * @Date 2022/9/11 20:34
 * @Version 1.0
 */
@Service
public class WXFootprintServiceImpl implements WXFootprintService {

    @Autowired
    MarketFootPrintMapper footPrintMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * @description: 显示用户浏览足迹 TODO:如何在足迹中不显示重复的商品
     * @param: id
     * @return: com.yang.bean.common.CommonData<com.yang.bean.MarketGoods>
     * @author zhangdongdong
     * @date: 2022/9/11 20:35
     */
    @Override
    public CommonData<MarketGoods> list(BaseParam info, Integer id) {

        //在market_footprint表中根据当前用户id查询其浏览过的商品的goodsId
        MarketFootPrintExample footPrintExample = new MarketFootPrintExample();
        footPrintExample.setDistinct(true);
        footPrintExample.setOrderByClause("add_time desc");
        footPrintExample.createCriteria().andUserIdEqualTo(id);
        List<MarketFootPrint> userFootprint = footPrintMapper.selectByExample(footPrintExample);


        List<Integer> ids = new ArrayList<>();
        PageHelper.startPage(info.getPage(), info.getLimit());
        for (MarketFootPrint footPrint : userFootprint) {
            ids.add(footPrint.getGoodsId());
        }
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.setOrderByClause("add_time desc");
        goodsExample.createCriteria().andIdIn(ids);
        List<MarketGoods> list = goodsMapper.selectByExample(goodsExample);

//        ArrayList<Integer> goodIds = new ArrayList<>();
//        PageHelper.startPage(info.getPage(), info.getLimit());
//        for (MarketFootPrint footPrint : userFootprint) {
//            //根据goodsId查询goods表得到的goods添加到list中
//            Integer goodsId = footPrint.getGoodsId();
//            goodIds.add(goodsId);
//        }
//        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
//        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
//        criteria.andIdIn(goodIds);
//        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /** 
     * @description: 删除足迹
     * @param: id
     * @return: void
     * @author zhangdongdong
     * @date: 2022/9/11 22:30
     */ 
    @Override
    public void delete(Integer id) {
        MarketFootPrintExample footPrintExample = new MarketFootPrintExample();
        footPrintExample.createCriteria().andGoodsIdEqualTo(id);
        footPrintMapper.deleteByExample(footPrintExample);
    }

}
